<template>
    <div>
         <div class="headstyle">
            <el-row :gutter="20">
                <el-col :span="16">
                    <div class="grid-content bg-purple">
                    <span class="locsty"><i class="el-icon-location iconsty"></i>当前位置：管理员账号管理>账号编辑</span>
                    </div>
                </el-col>        
                <el-col :span="8" class="loc-index">
                    <div class="grid-content bg-purple">
                        <el-button type="primary" plain @click="goTo('/adminmanage')" class="backbtn">返回</el-button>                   
                    </div>
                </el-col>
            </el-row>
        </div>
        <div class="contentstyle">
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="160px" class="demo-ruleForm">
              <el-form-item label="管理员账号" prop="te_number">
                  <el-input v-model="ruleForm.te_number" class="inputEdit"></el-input>
              </el-form-item>
              <el-form-item label="管理员姓名" prop="te_name">
                  <el-input v-model="ruleForm.te_name" class="inputEdit"></el-input>
              </el-form-item>
              <el-form-item label="所属部门" prop="te_depart">
                  <el-select v-model="ruleForm.te_depart" placeholder="请选择部门" class="selectsty">
                      <el-option
                        v-for="item in options"
                        :key="item.Te_depart"
                        :label="item.Te_depart"
                        :value="item.Te_depart"
                      ></el-option>                   
                  </el-select>
              </el-form-item>            
              <el-form-item label="所属一级业务维度" prop="te_kind">
                  <el-select v-model="ruleForm.te_kind" placeholder="请选择一级业务维度" class="selectsty">
                      <el-option label="管理平台" value="管理平台"></el-option>
                      <el-option label="华为业务" value="华为业务"></el-option>
                      <el-option label="网优业务" value="网优业务"></el-option>
                      <el-option label="中兴业务" value="中兴业务"></el-option>
                      <el-option label="工程业务" value="工程业务"></el-option>
                      <el-option label="代维业务" value="代维业务"></el-option>                 
                  </el-select>
              </el-form-item>              
              <el-form-item label="请输入密码" prop="te_psw">
                  <el-input v-model="ruleForm.te_psw" class="inputEdit" type="password"></el-input>
                  <el-tag style="color:red" v-show="isflag"> 如需修改请删除后重新输入，不作修改请勿操作</el-tag>
              </el-form-item>                       
              <el-form-item>                
                  <el-button type="primary btnsty" @click="submitForm('ruleForm')">确定</el-button>
                  <el-button type="primary btnsty" @click="goTo('/adminmanage')">关闭</el-button>
              </el-form-item>    
            </el-form>
        </div>
    </div>
</template>
<script>
export default {
  data() {
    var validatenumber = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请输入管理员账号"));
      } else {
        if (this.id != undefined) {
          if (this.number == value) {
            callback();
          } else {
            this.$axios.get("/Teacher/" + value + "/v2").then(json => {
              if (json.data) {
                callback();
              } else {
                callback(new Error("账号已存在，请更换！"));
              }
            });
          }
        }
        else {
            this.$axios.get("/Teacher/" + value + "/v2").then(json => {
              if (json.data) {
                callback();
              } else {
                callback(new Error("账号已存在，请更换！"));
              }
            });
          }
      }
    };
    return {
      id: this.$route.params.id,
      number: "",
      options: [],
      ruleForm: {
        te_number: "", //导师工号
        te_name: "", //导师姓名
        te_depart: "", //所属部门
        te_kind: "", //一级业务维度
        te_auth: 2, //添加部门管理员
        te_psw: ""
      },
      rules: {
        te_number: [
          //{ required: true, message: "请输入管理员账号", trigger: "blur" },
          { required: true, validator: validatenumber, trigger: "blur" }
        ],
        te_name: [
          { required: true, message: "请输入管理员姓名", trigger: "blur" }
        ],
        te_depart: [
          { required: true, message: "请选择所属部门", trigger: "change" }
        ],
        te_kind: [
          { required: true, message: "请选择业务维度", trigger: "change" }
        ],
        te_psw: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 8, message: "密码长度在不能小于8个字符", trigger: "blur" }
        ]
      },
      isflag: false
    };
  },
  mounted() {
    this.loadinfo();
    if (this.id != undefined) {
      this.isflag = true;
      this.$axios
        .get("/Teacher/" + this.id)
        .then(Jsondata => {
          this.number = Jsondata.data.Te_number;
          this.ruleForm.te_number = Jsondata.data.Te_number;
          this.ruleForm.te_name = Jsondata.data.Te_name;
          this.ruleForm.te_depart = Jsondata.data.Te_depart.toString();
          this.ruleForm.te_kind = Jsondata.data.Te_kind.toString();
          this.ruleForm.te_auth = Jsondata.data.Te_isadmin.toString();
          this.ruleForm.te_psw = Jsondata.data.Te_psw;
        })
        .catch(error => {});
    }
  },
  methods: {
    loadinfo() {
      this.$axios
        .get("/Teacher/1/v1")
        .then(json => {
          this.options = json.data;
        })
        .catch(error => {});
    },
    goTo(path) {
      this.$router.replace(path);
    },
    submitForm(formName) {
      if (this.id != undefined) {
        this.$refs[formName].validate(valid => {
          if (valid) {
            if (this.ruleForm.te_auth == "") {
              this.ruleForm.te_auth = 0;
            }
            var list = {
              Te_number: this.ruleForm.te_number,
              Te_name: this.ruleForm.te_name,
              Te_depart: this.ruleForm.te_depart,
              Te_kind: this.ruleForm.te_kind,
              Te_isadmin: this.ruleForm.te_auth,
              Te_psw: this.ruleForm.te_psw
            };
            this.$axios
              .put("/Teacher/" + this.id, list)
              .then(Jsondata => {
                if (Jsondata.data) {
                  this.$message({
                    type: "success",
                    message: "更新成功！"
                  });
                } else {
                  this.$message({
                    type: "error",
                    message: "更新失败，请重试或检查登录状态！"
                  });
                }
              })
              .catch(error => {});
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      } else {
        this.$refs[formName].validate(valid => {
          if (valid) {
            if (this.ruleForm.te_auth == "") {
              this.ruleForm.te_auth = 0;
            }
            var list = {
              Te_number: this.ruleForm.te_number,
              Te_name: this.ruleForm.te_name,
              Te_depart: this.ruleForm.te_depart,
              Te_kind: this.ruleForm.te_kind,
              Te_isadmin: this.ruleForm.te_auth,
              Te_psw: this.ruleForm.te_psw
            };
            this.$axios
              .post("/Teacher", list)
              .then(Jsondata => {               
                if (Jsondata.data!=0) {
                  this.$message({
                    type: "success",
                    message: "保存成功！"
                  });
                } else {
                  this.$message({
                    type: "error",
                    message: "更新失败，请重试或检查登录状态！"
                  });
                }
              })
              .catch(error => {});
          } else {
            console.log("error submit!!");
            return false;
          }
        });
      }
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.ywshowflag = false;
    }
  }
};
</script>

<style scoped>
/*.loc-index {
  padding-left: 150px !important;
}*/
.backbtn {
  width: 100px;
}
.headstyle {
  border-bottom: 2px solid #e5e5e5;
  padding-bottom: 20px;
}
.locsty {
  font-size: 16px;
  font-family: "Microsoft YaHei";
}
.iconsty {
  color: #53b9fe;
  font-size: 22px;
}
a {
  text-decoration: none;
  color: black;
}
.contentstyle {
  width: 75%;
  padding: 40px;
  padding-left: 100px;
}
.inputEdit {
  width: 48%;
}
.selectsty {
  width: 48%;
}
.btnsty {
  width: 85px;
}
</style>

